#include <iostream>

// 组件接口
class Component{
public:
    virtual ~Component() = default;
    virtual void operation() = 0;
};

// 具体组件
class ConcreteComponent : public Component{
public:
    void operation() override{
        std::cout << "[Core] ConcreteComponent operation." << std::endl;
    }
};

// 装饰器基类
class Decorator : public Component{
protected:
    Component *wrapped;
public:
    explicit Decorator(Component* component) : wrapped(component) {}
    void operation() override{
        if(wrapped) wrapped->operation();
    }
};

// 具体装饰器 A
class ConcreteDecoratorA : public Decorator{
public:
    ConcreteDecoratorA(Component* component) : Decorator(component){}
    void operation() override{
        Decorator::operation(); // wrapped component
        addedBehavior();
    }
private:
    void addedBehavior(){
        std::cout << "[Extend] added behaior by decorator A." << std::endl;
    }
};

// 具体装饰器 B
class ConcreteDecoratorB : public Decorator{
public:
    ConcreteDecoratorB(Component* component) : Decorator(component){}
    void operation() override{
        std::cout << "[Extend] added behavior by decorator B (before core)." << std::endl; 
        Decorator::operation(); // wrapped component
    }
};

int main()
{
    Component* component = new ConcreteComponent();

    component = new ConcreteDecoratorA(component);
    component = new ConcreteDecoratorB(component);

    std::cout << "=== client calling decorated component ===" << std::endl;
    component->operation();

    delete component;
}